//
//  ZGL24hVC.m
//  Faker
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGL24NewsVC.h"
#import "ZGLHTTPTOOL.h"
#import <MJRefresh.h>
#import "ZGLHotDetailsVC.h"

#import "ZGL24NewsModel.h"
#import "ZGL24NewsCell_bigPic.h"
#import "ZGL24NewsCell_normal.h"
#import "ZGL24NewsCell_photos.h"
@interface ZGL24NewsVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)NSArray *dicArr;
@property(nonatomic, assign)NSInteger cellheight;
@end

@implementation ZGL24NewsVC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = backColor;
    _table.backgroundColor = backColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)createTableView{
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height-113) style:UITableViewStylePlain];
    [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _table.tableFooterView = [[UIView alloc]init];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    
    [self.table registerClass:[ZGL24NewsCell_bigPic class] forCellReuseIdentifier:@"ZGL24NewsCell_bigPic"];
    [self.table registerClass:[ZGL24NewsCell_normal class] forCellReuseIdentifier:@"ZGL24NewsCell_normal"];
    __weak typeof(self) weakSelf = self;
    _table.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf handelData];
    }];
    [_table.header beginRefreshing];
}

- (void)handelData{
    NSString *url = @"http://c.m.163.com/nc/article/list/T1429173762551/0-20.html";
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:url withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        weakSelf.dicArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"T1429173762551"]];
        [weakSelf.table reloadData];
        [weakSelf.table.header endRefreshing];
    } withFail:^(NSError *error) {
        NSLog(@"没网:ZGL24hVC");
        [weakSelf.table.header endRefreshing];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGL24NewsModel *mod = [ZGL24NewsModel modWithDic:_dicArr[indexPath.row]];
    if (indexPath.row == 0) {
        ZGL24NewsCell_bigPic *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGL24NewsCell_bigPic"];
        cell.mod = mod;
        _cellheight = cell.height;
        return cell;
    }else{
        ZGL24NewsCell_normal *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGL24NewsCell_normal"];
        cell.mod = mod;
        _cellheight = cell.height;
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dicArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _cellheight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.view.userInteractionEnabled = NO;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZGL24NewsModel *mod = [ZGL24NewsModel modWithDic:self.dicArr[indexPath.row]];
    ZGLHotDetailsVC *hotDetails = [[ZGLHotDetailsVC alloc]init];
    hotDetails.docid = mod.docid;
    [self.navigationController pushViewController:hotDetails animated:YES];
    self.view.userInteractionEnabled = YES;
}

- (void)dealloc{
    _table.dataSource = nil;
    _table.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
